<!DOCTYPE html>
<html>

<head>
  <title>New extension: Hibernate Search + Elasticsearch</title>
  <script id="adobe_dtm" src="https://www.redhat.com/dtm.js" type="text/javascript"></script>
  <script src="/assets/javascript/highlight.pack.js" type="text/javascript"></script>
  <META HTTP-EQUIV='Content-Security-Policy' CONTENT="default-src 'none'; script-src 'self' 'unsafe-eval' 'sha256-ANpuoVzuSex6VhqpYgsG25OHWVA1I+F6aGU04LoI+5s=' 'sha256-ipy9P/3rZZW06mTLAR0EnXvxSNcnfSDPLDuh3kzbB1w=' js.bizographics.com https://www.redhat.com assets.adobedtm.com jsonip.com https://ajax.googleapis.com https://www.googletagmanager.com https://www.google-analytics.com https://use.fontawesome.com; style-src 'self' https://fonts.googleapis.com https://use.fontawesome.com; img-src 'self' *; media-src 'self' ; frame-src https://www.googletagmanager.com https://www.youtube.com; frame-ancestors 'none'; base-uri 'none'; object-src 'none'; form-action 'none'; font-src 'self' https://use.fontawesome.com https://fonts.gstatic.com;">
  <META HTTP-EQUIV='X-Frame-Options' CONTENT="DENY">
  <META HTTP-EQUIV='X-XSS-Protection' CONTENT="1; mode=block">
  <META HTTP-EQUIV='X-Content-Type-Options' CONTENT="nosniff">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Quarkus: Supersonic Subatomic Java">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:site" content="@QuarkusIO"> 
  <meta name="twitter:creator" content="@QuarkusIO">
  <meta property="og:url" content="https://quarkus.io/blog/hibernate-search-extension/" />
  <meta property="og:title" content="New extension: Hibernate Search + Elasticsearch" />
  <meta property="og:description" content="Quarkus: Supersonic Subatomic Java" />
  <meta property="og:image" content="/assets/images/quarkus_card.png" />
  <link rel="canonical" href="https://quarkus.io/blog/hibernate-search-extension/">
  <link rel="shortcut icon" type="image/png" href="/favicon.ico" >
  <link rel="stylesheet" href="https://quarkus.io/guides/stylesheet/config.css" />
  <link rel="stylesheet" href="/assets/css/main.css" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
  <link rel="alternate" type="application/rss+xml"  href="https://quarkus.io/feed.xml" title="Quarkus">
  <script src="https://quarkus.io/assets/javascript/goan.js" type="text/javascript"></script>
  <script src="https://quarkus.io/assets/javascript/hl.js" type="text/javascript"></script>
</head>

<body class="post">
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NJWS5L"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->

  <div class="nav-wrapper">
  <div class="grid-wrapper">
    <div class="width-12-12">
      <input type="checkbox" id="checkbox" />
      <nav id="main-nav" class="main-nav">
  <div class="container">
    <div class="logo-wrapper">
      
        <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_600px_reverse.png" class="project-logo" title="Quarkus"></a>
      
    </div>
    <label class="nav-toggle" for="checkbox">
      <i class="fa fa-bars"></i>
    </label>
    <div id="menu" class="menu">
      <span>
        <a href="/get-started/" class="">Get Started</a>
      </span>
      <span>
        <a href="/guides/" class="">Guides</a>
      </span>
      <span>
        <a href="/community/" class="">Community</a>
      </span>
      <span>
        <a href="/support/" class="">Support</a>
      </span>
      <span>
        <a href="/blog/" class="active">Blog</a>
      </span>
      <span>
        <a href="https://code.quarkus.io" class="button-cta secondary white">Start Coding</a>
      </span>
    </div>
  </div>
      </nav>
    </div>
  </div>
</div>

  <div class="content">
    <div class="post-page grid-wrapper">
  <div class="width-8-12 width-12-12-m doc-content">
    <div class="grid-wrapper">
      <div class="width-12-12">
        <p>
          <a href="/blog"><i class="fas fa-angle-left"></i> Back to all posts</a>
        </p>
      </div>
      <div class="width-12-12">
        <div class="post-date">
          June 18, 2019 
          
            <span class="tags"><a href="/blog/tag/extension">#extension</a><a href="/blog/tag/hibernate">#hibernate</a><a href="/blog/tag/elasticsearch">#elasticsearch</a></span>
          
        </div>
        <h1 class="post-title">New extension: Hibernate Search + Elasticsearch</h1>
        <div class="grid-wrapper">
          <div class="width-8-12 width-12-12-m byline-wrapper">
            
            
              <img class="headshot" src="https://www.gravatar.com/avatar/ec96387a1a8295b6fd6dffb68f80c352">
            
            <p class="byline">By Guillaume Smet</p>
          </div>
          <div class="width-12-12">
              <div class="paragraph">
<p>In the 0.15.0 version of Quarkus, we introduced the Hibernate Search + Elasticsearch extension and, with 0.16.0, we added <a href="/guides/hibernate-search-elasticsearch">a guide</a> and <a href="https://github.com/quarkusio/quarkus-quickstarts/tree/master/hibernate-search-elasticsearch">a quickstart</a> explaining how to use it.</p>
</div>
<div class="paragraph">
<p>If you want to index your Hibernate ORM entities in Elasticsearch and don&#8217;t mind updating your code from time to time, you&#8217;re very welcome to become an early adopter and provide feedback.</p>
</div>
<div class="sect1">
<h2 id="what-is-hibernate-search"><a class="anchor" href="#what-is-hibernate-search"></a>What is Hibernate Search?</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This is an old time favorite of mine.
Back in the days, when I was developing business applications for customers, discovering Hibernate Search really changed the way we implemented search engines and our customers were very happy with the search features we provided from that moment on.
I soon started to contribute to it and&#8230;&#8203; here I am.</p>
</div>
<div class="paragraph">
<p>With an additional dependency and some configuration, you end up being able to provide full text search features to your users, just by synchronizing your Hibernate ORM entities to a full text index.</p>
</div>
<div class="paragraph">
<p>And when I say full text search, I mean the real one, backed by <a href="http://lucene.apache.org/">Apache Lucene</a> or <a href="https://www.elastic.co/">Elasticsearch</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="why-is-it-better-with-quarkus"><a class="anchor" href="#why-is-it-better-with-quarkus"></a>Why is it better with Quarkus?</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The nice thing with using Hibernate Search on Quarkus is that, when compiling a native application with GraalVM, we build the Hibernate Search metamodel at compilation time.</p>
</div>
<div class="paragraph">
<p>Expect your native application to start faster than ever!</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="how-do-i-use-it"><a class="anchor" href="#how-do-i-use-it"></a>How do I use it?</h2>
<div class="sectionbody">
<div class="paragraph">
<p>It can&#8217;t be easier.</p>
</div>
<div class="paragraph">
<p>First, add the <code>quarkus-hibernate-search-elasticsearch</code> extension to your Quarkus application:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="shell" class="language-shell hljs">./mvnw quarkus:add-extension -Dextension=hibernate-search-elasticsearch</code></pre>
</div>
</div>
<div class="paragraph">
<p>(or you can directly add the <code>io.quarkus:quarkus-hibernate-search-elasticsearch</code> dependency to your <code>pom.xml</code>)</p>
</div>
<div class="paragraph">
<p>Then, take one of your Hibernate ORM entities and add a few annotations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">package org.acme.hibernate.search.elasticsearch.model;

import javax.persistence.Entity;

import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
@Indexed <i class="conum" data-value="1"></i><b>(1)</b>
public class Book extends PanacheEntity {

    @FullTextField(analyzer = "standard") <i class="conum" data-value="2"></i><b>(2)</b>
    public String title;
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Declare your entity as part of the full text index</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Add a full text field using the <code>standard</code> analyzer provided by Elasticsearch</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Then add some additional configuration to your <code>src/main/resources/application.properties</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">quarkus.hibernate-search.elasticsearch.version=7
quarkus.hibernate-search.elasticsearch.automatic-indexing.synchronization-strategy=searchable
quarkus.hibernate-search.elasticsearch.index-defaults.lifecycle.strategy=drop-and-create
quarkus.hibernate-search.elasticsearch.index-defaults.lifecycle.required-status=yellow</code></pre>
</div>
</div>
<div class="paragraph">
<p>I won&#8217;t give all the details here.
You should just know that this is a good configuration for testing, but definitely not a good one for production!</p>
</div>
<div class="paragraph">
<p>And that&#8217;s it.</p>
</div>
<div class="paragraph">
<p>From now on, if you start an Elasticsearch cluster locally with the default port, your <code>Book</code> entities will be indexed in this cluster.</p>
</div>
<div class="paragraph">
<p>And to search for them (isn&#8217;t it the whole point of it?), we provide an easy to use DSL:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">return Search.getSearchSession(em)
        .search(Book.class)
        .predicate(f -&gt;
            f.simpleQueryString().onFields("title").matching(pattern)
        )
        .fetchHits();</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="going-further"><a class="anchor" href="#going-further"></a>Going further</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This is just a small taste of what you can do with our Hibernate Search + Elasticsearch extension.</p>
</div>
<div class="paragraph">
<p>For instance, understanding the concept of text analysis is especially important for full text search and properly setting up analyzers depending on your requirements necessitates a bit more work.</p>
</div>
<div class="paragraph">
<p>You will find a lot more information about how you can set everything up properly in <a href="/guides/hibernate-search-elasticsearch">our extensive guide</a>.</p>
</div>
</div>
</div>
              
          </div>
          <div class="width-12-12"><div class="share-page">
  <a class="share-linkedin" href="https://www.linkedin.com/shareArticle?mini=true&url=https://quarkus.io/blog/hibernate-search-extension/&title=New extension: Hibernate Search + Elasticsearch" rel="nofollow" target="_blank" title="Share on LinkedIn">
    <img src="/assets/images/share-page/icons_social-linkedin.png"/>
  </a>
  <a class="share-twitter" href="https://twitter.com/intent/tweet?text=New extension: Hibernate Search + Elasticsearch&url=https://quarkus.io/blog/hibernate-search-extension/&via=quarkusio&related=quarkusio" rel="nofollow" target="_blank" title="Share on Twitter">
    <img src="/assets/images/share-page/icons_social-twitter.png"/>
  </a>
  <a class="share-facebook" href="https://facebook.com/sharer.php?u=https://quarkus.io/blog/hibernate-search-extension/" rel="nofollow" target="_blank" title="Share on Facebook">
    <img src="/assets/images/share-page/icons_social-facebook.png"/>
  </a>
  <a class="share-reddit" href="http://www.reddit.com/submit?url=https://quarkus.io/blog/hibernate-search-extension/" onclick="window.open(this.href, 'pop-up', 'left=20,top=20,width=900,height=500,toolbar=1,resizable=0'); return false;" title="Share on Reddit" >
    <img src="/assets/images/share-page/icons_social-reddit.png"/>
  </a>
  <a class="share-email" href="mailto:?subject=New extension: Hibernate Search + Elasticsearch&amp;body=New extension: Hibernate Search + Elasticsearch https://quarkus.io/blog/hibernate-search-extension/" title="Share via Email" >
    <img src="/assets/images/share-page/icons_social-email.png"/>
  </a>
</div>
</div>
        </div>
      </div>
    </div>
  </div>
</div>

  </div>

  <div class="content project-footer">
  <div class="footer-section">
    <div class="logo-wrapper">
      <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_reverse.svg" class="project-logo" title="Quarkus"></a>
    </div>
  </div>
  <div class="grid-wrapper">
    <p class="grid__item width-3-12">Quarkus is open. All dependencies of this project are available under the <a href='https://www.apache.org/licenses/LICENSE-2.0' target='_blank'>Apache Software License 2.0</a> or compatible license.<br /><br />This website was built with <a href='https://jekyllrb.com/' target='_blank'>Jekyll</a>, is hosted on <a href='https://pages.github.com/' target='_blank'>Github Pages</a> and is completely open source. If you want to make it better, <a href='https://github.com/quarkusio/quarkusio.github.io' target='_blank'>fork the website</a> and show us what you’ve got.</p>

    
      <div class="width-1-12 project-links">
        <span>Navigation</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="/">Home</a></li>
          
            <li><a href="/guides">Guides</a></li>
          
            <li><a href="/community/#contributing">Contribute</a></li>
          
            <li><a href="/faq">FAQ</a></li>
          
            <li><a href="/get-started">Get Started</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Contribute</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://twitter.com/quarkusio">Follow us</a></li>
          
            <li><a href="https://github.com/quarkusio">GitHub</a></li>
          
            <li><a href="/security">Security&nbsp;policy</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Get Help</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://groups.google.com/forum/#!forum/quarkus-dev">Forums</a></li>
          
            <li><a href="https://quarkusio.zulipchat.com">Chatroom</a></li>
          
        </ul>
      </div>
    

    
      <div class="width-3-12 more-links">
        <span>Quarkus is made of community projects</span>
        <ul class="footer-links">
          
            <li><a href="https://vertx.io/" target="_blank">Eclipse Vert.x</a></li>
          
            <li><a href="https://microprofile.io" target="_blank">Eclipse MicroProfile</a></li>
          
            <li><a href="https://hibernate.org" target="_blank">Hibernate</a></li>
          
            <li><a href="https://netty.io" target="_blank">Netty</a></li>
          
            <li><a href="https://resteasy.github.io" target="_blank">RESTEasy</a></li>
          
            <li><a href="https://camel.apache.org" target="_blank">Apache Camel</a></li>
          
            <li><a href="https://code.quarkus.io/" target="_blank">And many more...</a></li>
          
        </ul>
      </div>
    
  </div>
</div>
  <div class="content redhat-footer">
  <div class="grid-wrapper">
    <span class="licence">
      <i class="fab fa-creative-commons"></i><i class="fab fa-creative-commons-by"></i> <a href="https://creativecommons.org/licenses/by/3.0/" target="_blank">CC by 3.0</a> | <a href="https://www.redhat.com/en/about/privacy-policy">Privacy Policy</a>
    </span>
    <span class="redhat">
      Sponsored by
    </span>
    <span class="redhat-logo">
      <a href="https://www.redhat.com/" target="_blank"><img src="/assets/images/redhat_reversed.svg"></a>
    </span>
  </div>
</div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" integrity="sha384-8gBf6Y4YYq7Jx97PIqmTwLPin4hxIzQw5aDmUg/DDhul9fFpbbLcLh3nTIIDJKhx" crossorigin="anonymous"></script>
  <script type="text/javascript" src="/assets/javascript/mobile-nav.js"></script>
  <script type="text/javascript" src="/assets/javascript/scroll-down.js"></script>
  <script src="/assets/javascript/satellite.js" type="text/javascript"></script>
  <script src="https://quarkus.io/guides/javascript/config.js" type="text/javascript"></script>
  <script src="/assets/javascript/search-filter.js" type="text/javascript"></script>
  <script src="/assets/javascript/back-to-top.js" type="text/javascript"></script>
</body>

</html>
